import { mapMutations, mapActions, mapState } from "vuex";
import _ from "lodash";
import { typeClassName } from "@/pages/config/classNameMap";
import dayjs from "dayjs";
export default {
  data() {
    return {
      vms: {}, //页面组件vue实例
      models: {
        //模型
        person: {
          ...typeClassName["person"],
        },
      },
      onlyRead: false,

      dataSet: {
        //数据集
        tableData: [],
        formData: {},
        configFormData: {},
      },
    };
  },
  provide() {
    return {
      anc: this,
    };
  },
  computed: {},
  created() {
    Object.keys(this.models).map((k) => {
      this.models[k].clz = this.$modelObject.create(this.models[k].modelType);
      this.models[k].clzs = this.$modelObject.create(
        this.models[k].modelType + "Collection"
      );
    });
  },
  mounted() {
    this.$nextTick(() => {
      if (process.env.NODE_ENV === "development") {
        console.log(this);
      }
    });
  },
  methods: {
    /**
     *
     * @param {分页} pageNavigation
     * @param {筛选条件} filter
     * @param {*} orderBy
     * 页面加载方法
     */
    loadPage(
      pageNavigation,
      filter,
      hierValueCondition = "",
      orderBy = "",
      hiddenHierData,
      like = false
    ) {
      let newFilter = {
        ...filter,
        className: this.models.person.className,
        type: this.models.person.type,
      };
      this.vms.personTable.loading = true;
      this._$queryByPage(
        this.models["person"].clzs,
        pageNavigation,
        newFilter,
        hierValueCondition,
        orderBy,
        hiddenHierData
      ).then((res) => {
        this.vms.personTable.loading = false;
        // 整理数据 数据格式
        this.dataSet.tableData = this.models["person"].clzs.valueRecords;
        // this.vms.personTable.pageNavigation.total =
        //   this.models["person"].clzs.total;
      });
    },

    /**
     * 人员管理新增
     */
    handleCreate() {
      this.vms.createModal.show = true;
      this.onlyRead = false;
      this.dataSet.formData = this.$modelObject.getTpl(
        this.models.person.modelType
      ); // 这是一个整理接口数据JSON格式的方法
    },
    /**
     *
     * @param {*} row
     * 人员管理查看
     */
    handleDetail(row) {
      console.log(row);
      this.vms.detailModal.show = true;
      this.dataSet.formData = row;
      this.dataSet.configFormData = row.web;
      this.onlyRead = true;
    },
    /**
     *
     * @param {*} row
     * 人员管理编辑
     */
    handleEdit(row) {
      this.vms.editModal.show = true;
      this.onlyRead = false;
      this.dataSet.formData = row;
    },
    /**
     *
     * @param {行数据} row
     * @param {索引} index
     * 人员管理删除数据
     */
    handleDelete(row, index) {
      let _this = this;

      this._$deleteConfirm(() => {
       _this._$deleteByIds(_this.models.person.clzs, [String(row.id)]).then(
          (res) => {
            _this.models["person"].clzs.records.splice(index, 1);
            _this.dataSet.tableData =
              _this.models["person"].clzs.setValueRecords();
            _this.vms.personTable.pageNavigation.total--;
          }
        );
      });
    },
    /**
     *
     * @param {行数据} selection
     */
    handleDeleteMultiple(selection) {
      let _this = this;
      this._$deleteConfirm(() => {
        let ids = selection.map((item) => String(item.id));
        _this._$deleteByIds(_this.models.person.clzs, ids).then((res) => {
          _this.vms.personTable.pageNavigation.pageNumber = 1;
          _this.loadPage(
            _this.vms.personTable.pageNavigation,
            {},
            "",
            "",
            false
          );
        });
      });
    },

    // 创建人员管理提交
    createModalOk() {
      // this.vms.createView.validate((valid) => {
      this._$validateAll([this.vms.createView], (valid) => {
        if (valid) {
          this.dataSet.formData.className = this.models.person.className;
          this.dataSet.formData.type = this.models.person.type;
          this.dataSet.formData.name = +new Date();

          this._$add(this.models["person"].clz, this.dataSet.formData).then(
            (flag) => {
              if (flag) {
                this.vms.createView.loading = false;
                this.vms.createModal.show = false;
                this.vms.createView.resetFields();
                // this.vms.createEditor.resetFields();
                this.vms.personTable.loadPage(
                  this.vms.personTable.pageNavigation,
                  {},
                  "",
                  "",
                  false
                );
              }
            }
          );
        } else {
          this.$Message.info("请填写必填内容");
        }
      });
    },

    editModalOk() {
      this.vms.editView.validate((valid) => {
        if (valid) {
          this.models.person.clz.setRecord(this.dataSet.formData);
          this.models.person.clz.updateById().then((flag) => {
            if (flag) {
              this.vms.editView.loading = false;
              this.vms.editModal.show = false;
              // this.dataSet.configFormData = {};
              this.vms.personTable.loadPage(
                this.vms.personTable.pageNavigation,
                {},
                "",
                "",
                false
              );
            } else {
              this.vms.editView.loading = false;
            }
          });
        } else {
          // this.$Message.info("请填写必填内容");
          this._$requireInfo();
        }
      });
    },
    setOptionsByHier(
      classMapName,
      classObject,
      optionName,
      optionConfig = { label: "sf_name", value: "sf_code" },
      ref
    ) {
      let filter = {
        className: typeClassName[classMapName].className,
        type: typeClassName[classMapName].type,
      };
      let newFilter = "";
      if (this.tools.isReal(filter)) {
        newFilter = classObject.util.getFilter(filter);
      }
      let statusOptions = [];
      return classObject.query(newFilter).then((flag) => {
        if (flag) {
            classObject.record.hier.content.forEach((d) => {
            if (d.nodeType != "root" && d.parentId != 'root') {
              let option = {};
              for (let key in optionConfig) {
                option[key] = d.values[optionConfig[key]];
              }
              statusOptions.push(option);
            }
          });
        } else {
          statusOptions = [];
        }
        this.vms[ref][optionName] = statusOptions;
      });
    },
  },
};
